package com.graduate.twentyone.controller;

import com.graduate.twentyone.common.R;
import com.graduate.twentyone.domain.Cate;
import com.graduate.twentyone.service.CateService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Map;


@RestController
@Api(value = "cate控制类", tags = "cate管理相关")
@CrossOrigin
@RequestMapping("/cate")
public class CateController {

    @Autowired
    private CateService cateService;

    @RequiresPermissions("admin:all")
    @GetMapping("/all")
    public R queryAll() {
        return R.ok().data("catelist", cateService.queryAll());
    }

    @RequiresPermissions("admin:all")
    @PatchMapping("/update")
    public R updateById(@RequestBody Map map) {
        return R.ok().data("ans", cateService.updateById(
                new Cate((Integer) map.get("cate_id"), (String) map.get("cate_name"))));
    }

    @RequiresPermissions("admin:all")
    @PostMapping("/delete")
    public R deleteById(@RequestBody Map map) {
        cateService.deleteByIdAndFoods((Integer) map.get("cate_id"));
        return R.ok().data("ans", cateService.deleteById((Integer) map.get("cate_id")));
    }

    @RequiresPermissions("admin:all")
    @PostMapping("/add")
    public R addOne(@RequestBody Map map) {
        return R.ok().data("ans", cateService.addOne((String) map.get("cate_name")));
    }

    @RequiresPermissions("admin:all")
    @GetMapping("/echarts")
    public R queryEcharts() {
        return R.ok().data("echarts", cateService.queryEcharts());
    }
}

